
;(function($) {
	$.fn.fmatter.customActionLink = function(cellval, opts) {
		//将列配置保存在自定义的变量中
		var grid = $("#"+opts.gid)[0];
		opts.idName = grid.p.jsonReader.id;
		var op = {actionUrl: opts.colModel.actionUrl,tabTitle:opts.colModel.tabTitle, tabId: opts.colModel.tabId, actionTarget: opts.colModel.actionTarget, actionFunction: opts.colModel.actionFunction, idName: opts.idName},
		target = "", url;
		if(cellval == undefined){
			cellval="";
		}
		//单元格中的数据必须为字符串
		if(!$.StringUtils.isEmpty(cellval) || cellval == ""){
			//如果配置了URL，优先选择执行URL指定的路线
			if(op.actionUrl){
				//拼装URL
				url = App.ctx+op.actionUrl;
				if(url.indexOf("?") > 0){
					url = url+"&"+op.idName+"="+opts.rowId+"&"+opts.colModel.name+"="+cellval;
				}else{
					url = url+"?"+op.idName+"="+opts.rowId+"&"+opts.colModel.name+"="+cellval;
				}
				//指定是新建Tab还是当前页跳转
				if(op.actionTarget && op.actionTarget=='self'){
					return  "<a href='javascript:void(0)' onclick=javascript:location.href='"+url+"' ><span>"+cellval+"</span></a>";
				}else{
					return  "<a href='javascript:void(0)' onclick=window.parent.TabTagUtils.closeAndNewTab(\'"+op.tabId+"\',\'"+op.tabTitle+"\',\'"+url+"\')><span>"+cellval+"</span></a>";
				}				
			}else{
				//IE6 下void(0) 将导致onclick不执行
				if ($.browser.msie && $.browser.version <= 7){
					return  "<a onclick="+op.actionFunction+"('"+opts.rowId+"','"+cellval+"')><span>"+cellval+"</span></a>";
				}else{
					return  "<a href='javascript:void(0)' onclick="+op.actionFunction+"('"+opts.rowId+"','"+cellval+"')><span>"+cellval+"</span></a>";
					
				}
			}
		}
		//单元格数据不是字符串时使用默认的格式化器
		else{
			return  $.fn.fmatter.defaultFormat(cellval,opts);
		}
	};
	$.fn.fmatter.customValueText = function(cellval, opts){
		var text = cellval, valueText=[];
		//列配置了formatterValueText时，优先使用该数据
		if($.isArray(opts.colModel.formatterValueText)){
			valueText = opts.colModel.formatterValueText;
		}
		//如果未配置formatterValueText，将editoptions进行数据格式转换
		else if(opts.colModel.editoptions){
			var optionText = opts.colModel.editoptions.value;
			var optionArr = optionText.split(";");
			for(var i=0;i<optionArr.length;i++){
				var optionObj = {};
				optionObj.value = optionArr[i].substring(0,optionArr[i].indexOf(":"));
				optionObj.text = optionArr[i].substring(optionArr[i].indexOf(":")+1,optionArr[i].length);
				valueText.push(optionObj);
			}
		}
		//循环判断单元格的值与数组对象中各元素Value属性，相等时返回对应元素的Text属性
		for(var i=0;i<valueText.length;i++){
			var elm = valueText[i];
			if(i==0)
				text = elm.text;
			if(cellval != undefined && (cellval == elm.value || cellval === elm.value || cellval.toString() == elm.value)){
				text = elm.text;
				break;
			}
		}
		return  text;
	};
	$.fn.fmatter.customActionCol = function(cellval,opts){
		//从列配置中获取需要用到的属性配置，保存在自定义变量中
		var grid = $("#"+opts.gid)[0];
		opts.idName = grid.p.jsonReader.id;
		var op = {actionType:opts.colModel.actionType, actionTarget:opts.colModel.actionTarget, actionUrl:opts.colModel.actionUrl, tabId:opts.colModel.tabId, tabTitle:opts.colModel.tabTitle, idName: opts.idName, actionImg:opts.colModel.actionImg, actionText:opts.colModel.actionText, actionFunction:opts.colModel.actionFunction, name:opts.colModel.name};
		var img = "";
		var html = "";
		//删除动作列
		if(op.actionType == "delete"){
			img = "<img border='0' actionType='delete' alt='"+$.jgrid.nav.deltitle+"' src='"+App.ctx+"/images/icon/del.gif'/>";
			//返回拼装好的页面代码
			return  "<a href='javascript:void(0)' onclick=$(\'#"+opts.gid+"\').jqGrid(\'deleteRowById\',\'"+opts.rowId+"\') >"+img+"</a>";
		}
		//编辑动作列
		if(op.actionType == "edit"){
			img = "<img border='0' alt='"+$.jgrid.nav.edittitle+"' src='"+App.ctx+"/images/icon/change.gif'/>";
			//判断是否是弹出层编辑                             
			if(op.actionTarget == "openEditWindow"){
				return  "<a href='javascript:void(0)' onclick= $(\'#"+opts.gid+"\').jqGrid(\'setSelection\',\'"+opts.rowId+"\',"+true+");$(\'#"+opts.gid+"\').jqGrid(\'openEditWindow\')>"+img+"</a>";
			}else if(op.actionUrl){
				//组织URL，为URL添加参数
				var url_html = App.ctx+op.actionUrl;
				if(url_html.indexOf("?") > 0){
					url_html = url_html+"&"+op.idName+"="+opts.rowId;
				}else{
					url_html = url_html+"?"+op.idName+"="+opts.rowId;
				}
				//判断是打开新Tab还是当前页跳转
				if("self" == op.actionTarget){
					return  "<a href='javascript:void(0)' onclick=javascript:location.href='"+url_html+"' >"+img+"</a>";
				}else{
					return  "<a href='javascript:void(0)' onclick=window.parent.TabTagUtils.closeAndNewTab(\'"+op.tabId+"\',\'"+op.tabTitle+"\',\'"+url_html+"\')>"+img+"</a>";
				}
			}else{
				return  $.fn.fmatter.defaultFormat(cellval,opts);
			}
		}
		//用户自定义动作列
		if(op.actionType == "custom"){
			var actionHtml = "";
			//通过图片触发动作
			if(op.actionImg != null && op.actionImg.length > 0){
				actionHtml = "<img border='0' src='"+App.ctx+op.actionImg+"'/>";
			}
			//通过文本触发动作
			else if(op.actionText != null && op.actionText != ""){
				actionHtml = "<span>"+op.actionText+"</span>";
			}
			//配置URL属性时，优先选择URL指定操作
			if(op.actionUrl){
				if(op.actionTarget && op.actionTarget=='self'){
					return  "<a href='javascript:void(0)' onclick=javascript:location.href='"+App.ctx+op.actionUrl+"' >"+actionHtml+"</a>";
				}else{
					return  "<a href='javascript:void(0)' onclick=window.parent.TabTagUtils.closeAndNewTab(\'"+op.tabId+"\',\'"+op.tabTitle+"\',\'"+App.ctx+op.actionUrl+"\')>"+actionHtml+"</a>";
				}
			}
			//未配置URL，走Function指定的方法
			else if(!$.StringUtils.isEmpty(op.actionFunction)){
				//IE6 下void(0) 将导致onclick不执行
				if ($.browser.msie && $.browser.version <= 7){
					return  "<a onclick="+op.actionFunction+"('"+opts.rowId+"')>"+actionHtml+"</a>";
				}else{

					return  "<a href='javascript:void(0)' onclick=\"$('#"+opts.gid+"').jqGrid().restoreCellStatus();$('#"+opts.gid+"').jqGrid().resetSelection();$('#"+opts.gid+"').jqGrid().setSelection('"+opts.rowId+"', true, new $.Event('click', event));$.EventUtils.stopBubble(event);"+op.actionFunction+"('"+opts.rowId+"');\">"+actionHtml+"</a>";
				}
			}else{
				return  $.fn.fmatter.defaultFormat(cellval,opts);
			}
		}
		//$("'#"+opts.gid+"'").jqGrid('setRowData', opts.rowId, $.parseJSON("{\""+op.name+"\":\""+html+"\"}"));
	};
	$.fn.fmatter.customValueText.unformat = function(cellval,opts){
		var value, valueText=[];
		//配置formatterValueText属性时，优先使用该数据
		if($.isArray(opts.colModel.formatterValueText)){
			valueText = opts.colModel.formatterValueText;
		}
		//如果未配置formatterValueText，将editoptions进行数据格式转换
		else if(opts.colModel.editoptions){
			var optionText = opts.colModel.editoptions.value;
			var optionArr = optionText.split(";");
			for(var i=0;i<optionArr.length;i++){
				var optionObj = {};
				optionObj.value = optionArr[i].substring(0,optionArr[i].indexOf(":"));
				optionObj.text = optionArr[i].substring(optionArr[i].indexOf(":")+1,optionArr[i].length);
				valueText.push(optionObj);
			}
		}
		//单元格的值循环与valueText中各个元素的Text属性对比
		for(var i=0;i<valueText.length;i++){
			var elm = valueText[i];
			if(i==0)
				value = elm.value;
			if(cellval != undefined && cellval == elm.text){
				value = elm.value;
				break;
			}
		}
		return  value;
	};
})(jQuery);
